

#import "TextView.h"
#import "TextCell.h"

#define DEALY_WHEN_TITLE_IN_MIDDLE  5.0
#define DEALY_WHEN_TITLE_IN_BOTTOM  0.0

typedef NS_ENUM(NSUInteger, GYTitlePosition) {
    GYTitlePositionTop    = 1,
    GYTitlePositionMiddle = 2,
    GYTitlePositionBottom = 3
};

@interface TextView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *contentsAry;
@property (nonatomic, assign) BOOL shouldStop;         /*是否停止*/

@end

@implementation TextView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.shouldStop = NO;
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,  -40, frame.size.width, frame.size.height + 40) style: UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.showsHorizontalScrollIndicator = false;
        self.tableView.dataSource = self;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellAccessoryNone;
      if (!self.showNumber) {
        [self.tableView registerNib:[UINib nibWithNibName:@"TextCell" bundle:nil] forCellReuseIdentifier:@"TextCell"];
      }
      
        [self addSubview: self.tableView];

    }
    return self;
}

- (void)animationWithTexts:(NSMutableArray *)textAry {
  
    self.contentsAry = textAry;
    for (NSInteger i = 0; i < textAry.count; i++) {
      [self.contentsAry addObject:self.contentsAry[i]];
      // 这个位置的i根据显示来
      if (i == 12) {
        break;
      }
    }
  
    [self.contentsAry insertObject:self.contentsAry[0] atIndex:0];
    [self.tableView reloadData];

    [self startAnimation];
}

- (void)startAnimation {
  
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:0.1 delay:3 options:UIViewAnimationOptionCurveEaseInOut animations:^{

      CGPoint tableY = weakSelf.tableView.contentOffset;
    
      tableY.y += 40;
      weakSelf.tableView.contentOffset = tableY;
    } completion:^(BOOL finished) {
      if (self.tableView.contentSize.height > self.tableView.frame.size.height) {
        
        
        CGPoint offset = CGPointMake(0, self.tableView.contentSize.height - self.tableView.frame.size.height);
        
        if (self.tableView.contentOffset.y == offset.y) {
          [self.tableView setContentOffset:CGPointMake(0, 0)];
        }
        
      }
      if (!weakSelf.shouldStop) {
          [weakSelf startAnimation];
      }
    }];
}

- (void)stopAnimation {
    self.shouldStop = YES;
  
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
  if (self.showNumber) {
    return self.contentsAry.count;
  }
  
  else {
    return self.contentsAry.count / 2 + self.contentsAry.count % 2;
  }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
  if (self.showNumber) {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    cell.textLabel.text = self.contentsAry[indexPath.row];
      cell.textLabel.font = [UIFont systemFontOfSize:13];
    cell.textLabel.textColor = [UIColor whiteColor];
    return cell;
  }
  else {
    TextCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TextCell"];
    
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellAccessoryNone;
    cell.leftLabel.text = self.contentsAry[indexPath.row * 2];
     cell.textLabel.font = [UIFont systemFontOfSize:13];
    if (self.contentsAry.count % 2 == 0) {
      cell.rightLabel.text = self.contentsAry[indexPath.row * 2 + 1];
    }
    else {
      if (self.contentsAry.count / 2 == indexPath.row) {
        cell.rightLabel.text = @"";
      }
      else {
        cell.rightLabel.text = self.contentsAry[indexPath.row * 2 + 1];
      }
    }
    return cell;
  }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  return 40;
}

@end
